package com.fzu.normalservice.controller;


import com.fzu.commonutil.util.CodeEnum;
import com.fzu.commonutil.util.R;
import com.fzu.commonutil.entity.Housekeeper;
import com.fzu.commonutil.entity.User;
import com.fzu.normalservice.service.HousekeeperService;
import com.fzu.commonutil.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyq
 * @since 2023-03-26
 */
@RestController
@RequestMapping("/housekeeper")
@RequiredArgsConstructor
public class HousekeeperController {
    private final HousekeeperService housekeeperService;

    /**
     * 获取家政人员分页列表
     * @param housekeeper
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    public R list(Housekeeper housekeeper,
                  @RequestParam(required = false, defaultValue = "10") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "1") Integer pageSize,
                  @RequestHeader("token") String token) {
        User user = JwtUtil.getUserFromRedis(token);
        if (CodeEnum.COMPANY.getCode().equals(user.getRole())) {
            housekeeper.setCompanyId(user.getCompanyId());
        } else if (CodeEnum.CONSUMER.getCode().equals(user.getRole())) {
            housekeeper.setState(CodeEnum.FREE.getCode());
        }

        return R.ok().data(housekeeperService.list(housekeeper, pageNum, pageSize));
    }

    /**
     * 新增
     * @param housekeeper
     * @param token
     * @return
     */
    @PostMapping
    public R add(@RequestBody Housekeeper housekeeper,
                 @RequestHeader("token") String token) {
        User user = JwtUtil.getUser(token);
        housekeeperService.add(housekeeper, user);
        return R.ok();
    }

    /**
     * 修改
     * @param housekeeper
     * @param token
     * @return
     */
    @PutMapping
    public R update(@RequestBody Housekeeper housekeeper,
                    @RequestHeader("token") String token) {
        User user = JwtUtil.getUser(token);
        housekeeperService.update(housekeeper, user);
        return R.ok();
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") Integer id) {
        return R.ok().data(housekeeperService.detail(id));
    }


    /**
     * 获取家政员信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        return R.ok().data(housekeeperService.getById(id));
    }
}

